package com.shu.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shu.pojo.Article;
import com.shu.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import java.util.ArrayList;
import java.util.List;

/**
 * @author shu
 * @date 2021/3/23
 */

/**
 * 数据封装
 */
@Service
public class PagingUtils {


    @Autowired
    private ArticleService articleService;


    /**
     * 分页数据封装
     * @param tid
     * @param pages
     * @param model
     * @return
     */
    public  String Pages(Integer tid, Integer pages, Model model){
        //页码封装
        List<Integer> page_all=new ArrayList<>();
        QueryWrapper<Article> wrapper = new QueryWrapper<Article>();
        //分页数据
        Page<Article> articlePage = new Page<>(pages,10);
        wrapper.eq("tid",tid);
        Page<Article> list = articleService.page(articlePage, wrapper);
        //初始文章数据
        Article article = list.getRecords().get(0);
        for (int i = 1; i <list.getPages() ; i++) {
            page_all.add(i);
        }
        model.addAttribute("list",list);
        model.addAttribute("page",page_all);
        model.addAttribute("article",article);
        return "server";
    }
}
